package com.qf.contract.controller;

import com.qf.common.base.exception.ControllerException;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.FChangeRequest;
import com.qf.contract.qo.FChangeRequestQo;
import com.qf.contract.service.FChangeRequestService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

@Api(tags = "合同变更申请增删改查")
@RestController
@RequestMapping("/fchangerequestcontroller")
public class FChangeRequestController {
    @Resource
    private FChangeRequestService fChangeRequestService;

    @ApiOperation("新增合同变更申请")
    @PutMapping("/addfchangerequest")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "合同附件,请上传文件"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult addFChangeRequest(FChangeRequestQo fChangeRequestQo, MultipartFile file) {
        //设置默认时间
        try {
            //给个时间
            fChangeRequestQo.setChangeRequestDate(new Date());
            System.out.println(new Date());
            return RespResult.success(fChangeRequestService.addFChangeRequest(fChangeRequestQo, file));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("查询合同变更申请")
    @GetMapping("/listfchangerequest")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult listFChangeRequest(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) {
        //设置默认时间
        try {
            //给个时间
            return RespResult.success(fChangeRequestService.listFChangeRequest(current, size));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


    @ApiOperation("删除合同变更申请")
    @DeleteMapping("/delfchangerequest")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult delFChangeRequest(int id) {
        try {
            return RespResult.success(fChangeRequestService.delFChangeRequest(id));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


    @ApiOperation("修改合同变更申请")
    @PutMapping("/updatefchangerequest")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult updateFChangeRequest(FChangeRequest fChangeRequest) {
        try {

            return RespResult.success(fChangeRequestService.updateFChangeRequest(fChangeRequest));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


}
